//
//  GHSOD_THViewController.m
//  penzer
//
//  Created by 还带大道 on 2018/11/16.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "GHSOD_THViewController.h"
#import "GHSOD_SectionHeaderView.h"
#import "GHSOD_GoodsTableViewCell.h"
#import "GHSOD_BaseTableViewCell.h"
#import "GHSOD_AfterSaleSelectView.h"
#import "GHSOD_BtnView.h"

@interface GHSOD_THViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSMutableArray<SPSection *> *sections;
@property (nonatomic,weak) UITableView *tableView;
@property (nonatomic,strong) GHSOD_AfterSaleSelectView *afterSaleSelectView;
@property (nonatomic,strong) GHSOD_BaseTableViewCell *descTextViewCell;
@property (nonatomic,assign) GHSAfterSaleStatus status;
@property (nonatomic,strong) NSString *descStr;

@end

@implementation GHSOD_THViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self registerNoti];
    self.navigationItem.title = @"退货";
    _status = GHSAfterSaleStatus_TH_Nomal;
    [self initDataSource];
    [self setupTableView];
}



- (void)registerNoti
{
    [RZ_NOTIFICATIONCENTER addObserver:self selector:@selector(keyboardWillShowNoti:) name:UIKeyboardWillShowNotification object:nil];
    [RZ_NOTIFICATIONCENTER addObserver:self selector:@selector(keyboardWillHideNoti:) name:UIKeyboardWillHideNotification object:nil];
}


- (void)dealloc
{
    [RZ_NOTIFICATIONCENTER removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [RZ_NOTIFICATIONCENTER removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


- (void)keyboardWillShowNoti:(NSNotification *)noti
{
    //取出键盘最终的frame
    CGRect rect = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    //取出键盘弹出需要花费的时间
    double duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        CGFloat y = self.tableView.contentOffset.y + rect.size.height;
        self.tableView.contentOffset = CGPointMake(0,y);
    }];
    
}


- (void)keyboardWillHideNoti:(NSNotification *)noti
{
    //取出键盘最终的frame
    CGRect rect = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    //取出键盘弹出需要花费的时间
    double duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        CGFloat y = self.tableView.contentOffset.y - rect.size.height;
        self.tableView.contentOffset = CGPointMake(0,y);
    }];
    self.descStr = self.descTextViewCell.desc.text;
}



- (void)initDataSource
{
    NSArray *items;
    [_sections removeAllObjects];
    
    switch (_status) {
        case GHSAfterSaleStatus_TH_Nomal:{
            items = @[@"商品信息",@"买家信息",@"订单信息",@"退货退款"];
            break;
        }
            
        case GHSAfterSaleStatus_TH_Refuse:{
            items = @[@"商品信息",@"买家信息",@"订单信息",@"退货退款",@"拒绝退货原因"];
            break;
        }
            
        case GHSAfterSaleStatus_TH_Agree:{
            items = @[@"商品信息",@"买家信息",@"订单信息",@"退货退款",@"买家退货"];
            break;
        }
            
        case GHSAfterSaleStatus_TH_Finished:{
            items = @[@"商品信息",@"买家信息",@"订单信息",@"退货退款",@"买家退货",@"退款明细"];
            break;
        }
            
        default:
            break;
    }
    
    [items enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        SPSection *sec = [SPSection new];
        sec.items = [NSMutableArray array];
        sec.title = obj;
        sec.open = YES;
        [self.sections addObject:sec];
        switch (idx) {
            case 0:{//商品信息
                [sec.items addObject:@"商品信息"];
                break;
            }
            case 1:{//买家信息
                [sec.items addObjectsFromArray:@[@"姓名",@"电话",@"地址"]];
                break;
            }
            case 2:{//订单信息
                [sec.items addObjectsFromArray:@[@"订单编号",@"付款时间",@"发货时间",@"收货时间"]];
                break;
            }
            case 3:{//退货退款
                [sec.items addObjectsFromArray:@[@"申请时间",@"退货原因",@"退货说明"]];
                break;
            }
            case 4:{
                if(self.status == GHSAfterSaleStatus_TH_Refuse){//拒绝退货
                    [sec.items addObjectsFromArray:@[@"拒绝退货"]];
                }else{
                    [sec.items addObjectsFromArray:@[@"退货编号",@"快递单号"]];
                }
                break;
            }
            case 5:{//退款明细
                [sec.items addObjectsFromArray:@[@"退款原因",@"退款金额",@"退款路径",@"退款说明"]];
                break;
            }
        }
    }];
}




- (void)setupTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView =tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tableView];
    
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSOD_SectionHeaderView class]) bundle:nil]  forHeaderFooterViewReuseIdentifier:NSStringFromClass([GHSOD_SectionHeaderView class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSOD_GoodsTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GHSOD_GoodsTableViewCell class])];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GHSOD_BaseTableViewCell class]) bundle:nil]  forCellReuseIdentifier:NSStringFromClass([GHSOD_BaseTableViewCell class])];
    
}


#pragma mark--UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _sections.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    SPSection *sec = self.sections[section];
    return sec.isOpen?sec.items.count:0;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}



- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    CGFloat height = 0.01;
    switch (section) {
        case 3:{
            if(self.sections[section].isOpen){
                self.afterSaleSelectView.hidden = !self.sections[section].isOpen;
                height = self.afterSaleSelectView.contentH;
            }
            break;
        }
        case 4:{
            height = 90;
            break;
        }
        case 5:{
            height = 90;
            break;
        }
    }
    return height;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 0;
    if(indexPath.section == 0){
        height = 160;
    }else{
        if(_status == GHSAfterSaleStatus_TH_Refuse && indexPath.section == 4){//拒绝退货原因
            height = 100;
        }else{
            height = 60;
        }
    }
    return height;
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    SPSection *sec = _sections[section];
    GHSOD_SectionHeaderView *headerV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass([GHSOD_SectionHeaderView class])];
    headerV.title.text = sec.title;
    headerV.arrow.highlighted = sec.open;
    headerV.arrow.hidden = NO;
    headerV.arrow.hidden = (section==0 || section == 4 || section == 5);
    __weak typeof(headerV) weakO = headerV;
    headerV.onClickBlock = ^{
        if(weakO.arrow.hidden) return;
        sec.open = !sec.isOpen;
        weakO.arrow.highlighted = sec.open;
        [tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
    };
    return headerV;
}



- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    switch (section) {
        case 3:{//退货信息
            if(self.sections[section].isOpen){
                self.afterSaleSelectView.status = _status;
                self.afterSaleSelectView.btn1.userInteractionEnabled = (_status == GHSAfterSaleStatus_TH_Nomal);
                self.afterSaleSelectView.btn2.userInteractionEnabled = (_status == GHSAfterSaleStatus_TH_Nomal);
                self.afterSaleSelectView.btn1.hidden = (_status == GHSAfterSaleStatus_TH_Refuse);
                self.afterSaleSelectView.btn2.hidden = (_status == GHSAfterSaleStatus_TH_Agree || _status == GHSAfterSaleStatus_TH_Finished);
                view = self.afterSaleSelectView;
            }
            break;
        }
        case 4:{//买家退货
            GHSOD_BtnView *btnV = [GHSOD_BtnView shareView];
            btnV.btnOnClickBlock = ^{
                if(self.status == GHSAfterSaleStatus_TH_Agree){
//                    RZLog(@"去发货");
                }else{//拒绝
                    RZLog(@"发送");
                }
            };
            
            NSString *title;
            UIColor *color = [UIColor whiteColor];
            if(self.status == GHSAfterSaleStatus_TH_Agree){
                title = @"待收货";
                color = RZ_COLOR(81, 81, 81);
                btnV.btn.userInteractionEnabled = NO;
                [btnV.btn setBackgroundImage:[UIImage imageNamed:@"按钮10"] forState:UIControlStateNormal];
                
            }else if(self.status == GHSAfterSaleStatus_TH_Refuse){
                title = @"发送";
            }else if (self.status == GHSAfterSaleStatus_TH_Finished){
                title = @"已收货";
                color = RZ_COLOR(81, 81, 81);
                btnV.btn.userInteractionEnabled = NO;
                [btnV.btn setBackgroundImage:[UIImage imageNamed:@"按钮10"] forState:UIControlStateNormal];
            }
            
            btnV.btn.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:18];
            [btnV.btn setTitle:title forState:UIControlStateNormal];
            [btnV.btn setTitleColor:color forState:UIControlStateNormal];
            view = btnV;
            break;
        }
        case 5:{
            GHSOD_BtnView *btnV = [GHSOD_BtnView shareView];
            btnV.btn.userInteractionEnabled = NO;
            btnV.btn.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:18];
            [btnV.btn setTitle:@"已完成" forState:UIControlStateNormal];
            [btnV.btn setTitleColor:RZ_COLOR(81, 81, 81) forState:UIControlStateNormal];
            [btnV.btn setBackgroundImage:[UIImage imageNamed:@"按钮10"] forState:UIControlStateNormal];
            view = btnV;
            break;
        }
    }
    return view;
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SPSection *sec = self.sections[indexPath.section];
    UITableViewCell *cell;
    if(indexPath.section == 0){
        GHSOD_GoodsTableViewCell *goodsCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GHSOD_GoodsTableViewCell class])];
        cell = goodsCell;
        
    }else{
        
        GHSOD_BaseTableViewCell *baseCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([GHSOD_BaseTableViewCell class])];
        cell = baseCell;
        //重置数据
        baseCell.desc.editable = NO;
        baseCell.desc.text = @"";
        baseCell.desc.font = [UIFont systemFontOfSize:12];
        [baseCell.rightBtn setTitle:@"" forState:UIControlStateNormal];
        [baseCell.rightBtn setImage:nil forState:UIControlStateNormal];
        
        
        if(indexPath.row < sec.items.count){
            baseCell.title.text = sec.items[indexPath.row];
        }
        
        switch (indexPath.section) {
            case 0:{//商品信息
                break;
            }
                
            case 1:{//买家信息
                if(indexPath.row == 0){//姓名--联系用户
                    baseCell.desc.text = @"高灵芝";
                    [baseCell.rightBtn setImage:[UIImage imageNamed:@"icon_ghs_contact"] forState:UIControlStateNormal];
                    baseCell.rightBtnOnClickBlock = ^{
                        RZLog(@"姓名--联系用户");
                    };
                }else if (indexPath.row == 1){
                    baseCell.desc.text = @"18696164227";
                }else{
                    baseCell.desc.text = @"昆明市官渡区和平新村502";
                }
                break;
            }
                
            case 2:{//订单信息
                if(indexPath.row == 0){//订单编号--复制
                    baseCell.desc.text = @"2226855021594056785";
                    [baseCell.rightBtn setTitle:@"复制" forState:UIControlStateNormal];
                    baseCell.rightBtnOnClickBlock = ^{
                        RZLog(@"订单编号--复制");
                    };
                }else if (indexPath.row == 1){
                    baseCell.desc.text = @"2018-10-05  21:40:25";
                }else if (indexPath.row == 2){
                    baseCell.desc.text = @"2018-10-05  21:40:25";
                }else{
                    baseCell.desc.text = @"2018-10-05  21:40:25";
                }
                break;
            }
                
            case 3:{//退货退款
                if(indexPath.row == 1){
                    baseCell.desc.textColor = RZ_COLOR(255, 44, 44);
                    baseCell.desc.text = @"其他";
                }else if (indexPath.row == 2){
                    baseCell.desc.textColor = RZ_COLOR(51, 51, 51);
                    baseCell.desc.text = @"不想要了";
                }else{
                    baseCell.desc.textColor = RZ_COLOR(51, 51, 51);
                    baseCell.desc.text = @"2018-10-05  21:40:25";
                }
                break;
            }
                
            case 4:{
                baseCell.desc.textColor = RZ_COLOR(51, 51, 51);
                if(self.status == GHSAfterSaleStatus_TH_Refuse){//拒绝退货
                    baseCell.desc.editable = YES;
                    baseCell.desc.text = self.descStr;
                    self.descTextViewCell = baseCell;
                    
                }else{//同意退货
                    if(indexPath.row == 0){
                        baseCell.desc.text = @"T52256333266633";
                    }else{
                        baseCell.desc.text = @"顺丰  SF9765490";
                        [baseCell.rightBtn setImage:[UIImage imageNamed:@"查看物流2"] forState:UIControlStateNormal];
                        baseCell.rightBtnOnClickBlock = ^{
                            RZLog(@"快递单号--查看物流");
                        };
                    }
                }
                break;
            }
                
                
            case 5:{//退款明细
                baseCell.desc.textColor = RZ_COLOR(51, 51, 51);
                if(indexPath.row == 0){//退款原因
                    baseCell.desc.text = @"不想要了";
                    
                }else if (indexPath.row == 1){//退款金额
                    if(_status == GHSAfterSaleStatus_HH_Finished){
                        baseCell.desc.editable = YES;
                    }else{
                        baseCell.desc.editable = YES;
                    }
                    baseCell.desc.text = @"¥ 4300 元";
                    baseCell.desc.textColor = RZ_COLOR(255, 30, 30);
                    baseCell.desc.font = [UIFont systemFontOfSize:15];
                    [baseCell.rightBtn setImage:[UIImage imageNamed:@"编辑价格"] forState:UIControlStateNormal];
                    baseCell.rightBtnOnClickBlock = ^{
                        RZLog(@"编辑价格");
                    };
                    
                }else if (indexPath.row == 2){//退款路径
                    baseCell.desc.text = @"原路退回";
                    
                }else{//退款说明
                    baseCell.desc.editable = YES;
                    baseCell.desc.placeholder = @"选填";
                }
                break;
            }
        }
    }
    return cell;
}



#pragma mark--bottomBtnOnClick
- (void)bottomBtnOnClick
{
    RZLog(@"bottomBtnOnClick");
}



#pragma mark--setter&&getter

- (NSMutableArray<SPSection *> *)sections
{
    if(!_sections){
        _sections = [NSMutableArray array];
    }
    return _sections;
}




- (GHSOD_AfterSaleSelectView *)afterSaleSelectView
{
    if(!_afterSaleSelectView){
        _afterSaleSelectView = [GHSOD_AfterSaleSelectView shareView];
        __weak typeof(self) weakS = self;
        __weak typeof(_afterSaleSelectView) weakO = _afterSaleSelectView;
        _afterSaleSelectView.itemOnClick = ^(NSInteger index){
            if(index == 0){//同意换货
                if(weakS.status == GHSAfterSaleStatus_TH_Agree) return;
                weakS.status = GHSAfterSaleStatus_TH_Agree;
                
            }else if (index==1){//拒绝退货
                if(weakS.status == GHSAfterSaleStatus_TH_Refuse) return;
                weakS.status = GHSAfterSaleStatus_TH_Refuse;
                
            }else if(index == 2){//查看物流
                if(weakS.status == GHSAfterSaleStatus_TH_Finished) return;
                weakS.status = GHSAfterSaleStatus_TH_Finished;
            }
            
            weakO.status = weakS.status;
            [weakS initDataSource];
            [weakS.tableView reloadData];
        };
        
    }
    return _afterSaleSelectView;
}

@end
